<?php

/*
 * 测试策略模式
 */

namespace App\Controller;

use App\DesignPattern\Strategy\MaleUserStrategy;
use App\DesignPattern\Strategy\FemaleUserStrategy;
use App\DesignPattern\Strategy\UserStrategyFactory;

class ControllerStrategy
{
    /**
     * 为方便IDE自动提示，该变量类型为MaleUserStrategy，实际上是实现了UserStrategy接口的类
     * @var MaleUserStrategy
     */
    private $user = null;
	
	public function actionBad()
    {
	    //硬编码
	    if (isset($_GET['female']))
        {
            $user = new FemaleUserStrategy();
        }
        else
        {
            $user = new  MaleUserStrategy();
        }
        //如果还有其他的分支，则又需要再此处进行添加
        $user->showAD();
	    $user->showGoods();
	}

	private function setUser()
    {
        $type = isset($_GET['female']) ? 'female' : 'male';
        $this->user = UserStrategyFactory::create($type);
    }

    public function actionGood()
    {
        $this->setUser();
        $this->user->showAD();
        $this->user->showGoods();
    }
}
